﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.achievements {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.utils.*;
    import Classes.util.*;
    import Modules.achievements.data.*;
    import Modules.stats.types.*;
    import Modules.achievements.transactions.*;
    import Modules.achievements.events.*;

    public class AchievementManager extends EventDispatcher {

        protected var m_isUpdated:Boolean;
        protected var m_groups:Dictionary;

        public function AchievementManager(){
            this.m_groups = new Dictionary();
        }
        public function loadServerData(_arg1:Object, _arg2:Object, _arg3:Boolean):void{
            var _local4:String;
            var _local5:Object;
            var _local6:Object;
            var _local7:XML;
            if (_arg1 == null){
                return;
            };
            this.m_isUpdated = ((this.m_isUpdated) || (_arg3));
            for (_local4 in _arg1) {
                _local5 = _arg1[_local4];
                _local6 = (((_arg2) && (_arg2[_local4]))) ? _arg2[_local4] : {};
                if (this.m_groups[_local4] == null){
                    _local7 = Global.gameSettings.getAchievementGroupByName(_local4);
                    if (_local7 != null){
                        this.m_groups[_local4] = new AchievementGroup(_local7);
                    };
                };
                if (this.m_groups[_local4] != null){
                    (this.m_groups[_local4] as AchievementGroup).update(_local5, _local6);
                };
            };
            dispatchEvent(new AchievementGroupsUpdatedEvent(AchievementGroupsUpdatedEvent.UPDATED));
        }
        public function get isUpdated():Boolean{
            return (this.m_isUpdated);
        }
        public function purchaseFinish(_arg1:String, _arg2:String):Boolean{
            var _local3:Achievement = this.getAchievement(_arg1, _arg2);
            if (_local3 == null){
                return (false);
            };
            var _local4:Boolean = (((_local3.state == Achievement.UNINIT)) || ((_local3.state == Achievement.STARTED)));
            if (((!(_local4)) || ((Global.player.cash < _local3.cashcost)))){
                return (false);
            };
            GameTransactionManager.addTransaction(new TAchievementsPurchaseFinish(_arg1, _arg2));
            Global.player.cash = (Global.player.cash - _local3.cashcost);
            this.setAchievementState(_arg1, _arg2, Achievement.FINISHED);
            StatsManager.count(StatsCounterType.ACHIEVEMENTS, StatsKingdomType.ACH_PURCHASE_FINISH, _arg1, _arg2);
            return (true);
        }
        public function claimReward(_arg1:String, _arg2:String):Boolean{
            var _local3:Achievement = this.getAchievement(_arg1, _arg2);
            if ((((_local3 == null)) || (!((_local3.state == Achievement.FINISHED))))){
                return (false);
            };
            GameTransactionManager.addTransaction(new TAchievementsReward(_arg1, _arg2));
            Global.player.giveRewards(_local3.rewards, true);
            this.setAchievementState(_arg1, _arg2, Achievement.REWARDED);
            StatsManager.count(StatsCounterType.ACHIEVEMENTS, StatsKingdomType.ACH_CLAIM_REWARD, _arg1, _arg2);
            return (true);
        }
        protected function setAchievementState(_arg1:String, _arg2:String, _arg3:String):void{
            var _local4:AchievementGroup = this.m_groups[_arg1];
            if (_local4 == null){
                return;
            };
            var _local5:Achievement = _local4.getAchievement(_arg2);
            if (_local5 == null){
                return;
            };
            _local5.state = _arg3;
            _local5.justUpdated = true;
            dispatchEvent(new AchievementUpdatedEvent(AchievementUpdatedEvent.UPDATED, _arg1, _arg2));
        }
        public function startFullUpdate():void{
            GameTransactionManager.addTransaction(new TAchievementsUpdate(""));
        }
        public function startGroupUpdate(_arg1:String):void{
            GameTransactionManager.addTransaction(new TAchievementsUpdate(_arg1));
        }
        public function getAchievementGroups():Dictionary{
            return (this.m_groups);
        }
        public function getAchievementGroup(_arg1:String):AchievementGroup{
            return (this.m_groups[_arg1]);
        }
        public function getAchievement(_arg1:String, _arg2:String):Achievement{
            return (((this.m_groups[_arg1])==null) ? null : (this.m_groups[_arg1] as AchievementGroup).getAchievement(_arg2));
        }

    }
}//package Modules.achievements 
